package com.sam.im.sms.base;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;

import com.sam.im.sms.R;
import com.sam.im.sms.mvp.presenters.BasePresenter;
import com.sam.im.sms.mvp.views.IBaseView;
import com.sam.im.sms.util.Utils;

import java.util.Locale;

/**
 * 描述
 * liuyucheng
 * Created By 刘育成 on 2019/5/31.
 */
public abstract class BaseActivity extends AppCompatActivity implements IBaseView {
    private BasePresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setMyLanguage();
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());
        mPresenter = new BasePresenter(this);
        init(savedInstanceState);


    }

    private void setMyLanguage() {
        String language = Utils.saveLanguage(BaseActivity.this, "", 2);
        Locale myLocale = new Locale(Locale.getDefault().getLanguage());
        if ("1".equals(language)) {
            myLocale = Locale.SIMPLIFIED_CHINESE;
        } else if ("2".equals(language)) {
            myLocale = Locale.TRADITIONAL_CHINESE;
        } else if ("3".equals(language)) {
            myLocale = Locale.ENGLISH;
        } else if ("4".equals(language)) {
            myLocale = Locale.JAPANESE;
        } else if ("5".equals(language)) {
            myLocale = Locale.KOREAN;
        }

        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
    }

    @Override
    public abstract int getContentViewId();

    @Override
    public abstract void init(Bundle savedInstanceState);

    @Override
    public <T extends View> T getView(int id) {
        return mPresenter.getView(id);
    }

    @Override
    public void showToast(String message) {
        mPresenter.showToast(message);
    }

    @Override
    public void startActivity(Class clazz) {
        mPresenter.startActivity(clazz, null, false);//由于vivo的页面切换不友好，这里舍弃
    }

    @Override
    public void startActivity(Class clazz, boolean isAnim) {
        mPresenter.startActivity(clazz, null, false);//由于vivo的页面切换不友好，这里舍弃
    }

    @Override
    public void startActivity(Class clazz, Bundle bundle) {
        mPresenter.startActivity(clazz, bundle, false);//由于vivo的页面切换不友好，这里舍弃
    }

    @Override
    public void startActivity(Class clazz, Bundle bundle, boolean isAnim) {
        mPresenter.startActivity(clazz, bundle, false);//由于vivo的页面切换不友好，这里舍弃
    }

    @Override
    public void startActivityForResult(Class clazz, int requestCode) {
        mPresenter.startActivityForResult(clazz, requestCode, null);
    }

    @Override
    public void startActivityForResult(Class clazz, int requestCode, Bundle bundle) {
        mPresenter.startActivityForResult(clazz, requestCode, bundle);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(0, R.anim.af_right_out);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
